package com.cgx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.cgx.annotation.ResultHandler;
import com.cgx.dao.CityDao;
import com.cgx.entity.City;
import com.cgx.vo.RequestObject;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/**
 * @author chenguanxin
 * @date 2021-02-19 22:50
 */
@Controller
@CrossOrigin("*")
@RequestMapping("/city")
public class CityController {

    @Autowired
    private CityDao cityDao;

    @RequestMapping("/save")
    @ResponseBody
    @ResultHandler
    public Object save() throws Exception {
        //  读取地址数据
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\xin\\Desktop\\address.txt"));
        StringBuffer sb = new StringBuffer();
        String line = null;
        while((line = br.readLine()) != null){
            sb.append(line);
        }
        //  地址数据转为java对象
        List<Map<String,Object>> listMap = JSON.parseObject(sb.toString(),new TypeReference<List<Map<String,Object>>>(){});
        System.out.println(JSON.toJSONString(listMap));
        Map<String, Map<String,Object>> allCityCode = new HashMap<>();
        for (Map<String,Object> map : listMap) {
            allCityCode.put(map.get("code").toString(), map);
        }
        List<City> cityList = new ArrayList<>();
        for (Map<String,Object> map : listMap) {
            String code = map.get("code").toString();
            String latitude = map.get("latitude").toString();
            String longitude = map.get("longitude").toString();
            String name = map.get("name").toString();
            String parentCode = map.get("parentCode").toString();
            City city = new City();
            //  设置id
            city.setId(UUID.randomUUID().toString());
            //  设置城市编码
            city.setCode(code);
            //  设置维度
            city.setLatitude(latitude);
            //  设置经度
            city.setLongitude(longitude);
            //  设置城市名
            city.setCityName(name);
            //  拼装城市全名
            while (!"0".equals(parentCode) && allCityCode.get(parentCode) != null) {
                Map<String, Object> parentMap = allCityCode.get(parentCode);
                parentCode = parentMap.get("parentCode").toString();
                name = parentMap.get("name").toString() + name;
            }
            //  设置城市全名
            city.setCityAllName(name);
            //  跳过经度或者纬度为空的城市
            if (StringUtils.isBlank(latitude) || StringUtils.isBlank(longitude)) {
                continue;
            }
            cityList.add(city);
        }
        //  持久化数据
        for (City city : cityList) {
            cityDao.save(city);
        }
        return "success";
    }

    @RequestMapping("/query")
    @ResponseBody
    @ResultHandler
    public Object query(@RequestBody RequestObject requestObject) {
        //  获取搜索条件
        String cityName = (String) requestObject.getData().get("cityName");
        //  拼装搜索条件
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("cityAllName", cityName);
        //  搜索
        Iterable<City> search = cityDao.search(matchQueryBuilder);
        List<City> cityList = new ArrayList<>();
        search.forEach(cityList::add);
        //  取匹配度最高的前5个地址
        if (cityList.size() > 5) {
            cityList = cityList.subList(0, 5);
        }
        //  返回数据
        return cityList;
    }
}
